package com.base.common.interceptor;


import com.base.common.exception.BizException;
import com.base.common.util.BaseResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 异常统一处理
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 捕获业务异常
     */
    @ExceptionHandler(BizException.class)
    public BaseResponse<?> handleBizException(BizException e) {
        return BaseResponse.fail(e.getCode(), e.getMessage());
    }


    /**
     * 捕获所有未知异常
     */
    @ExceptionHandler(Exception.class)
    public BaseResponse<?> handleException(Exception e) {
        e.printStackTrace();  // 打印日志
        return BaseResponse.fail("系统异常：" + e.getMessage());
    }


}
